﻿//创建时间: 2010-12-15
//创建作者: 李城
//功能说明: JS通用类         
//最后修改: 2010-12-15 
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Newtonsoft.Json;
using System.Text.RegularExpressions;
using System.Collections;
using System.Data;

namespace TRPOP.Common
{
    /// <summary>
    /// JS操作通用方法
    /// </summary>
    public class JsUtil
    {
        /// <summary>
        /// 整个文件操作的 Encoding 类型
        /// </summary>
        private static System.Text.Encoding oEncoding = System.Text.Encoding.UTF8;
        private static object objLock = new object();
        /// <summary>
        /// 单词查询数据限制为50条
        /// </summary>
        public static int JsonDataLimit(int InitNum,int intMaxNum)
        {
            if (InitNum == 0)
                return 0;
            if (InitNum >= intMaxNum)
            {
                return intMaxNum >= 1000 ? 1000 : intMaxNum;
            }
            return InitNum;
        }
        /// <summary>
        /// document.write HTML时，对特殊字符进行处理
        /// </summary>
        /// <param name="strHTML"></param>
        public static void JSDocWriteHTMLFormat(ref string strHTML)
        {
            string[] strOldList = { "\t", "\r", "\n", "/", "\"" };
            string[] strNewList = { "  ", "  ", "  ", "\\/", "\\\"" };
            if (strOldList.Length < strNewList.Length)
                return;
            for (int i = 0; i < strOldList.Length; i++)
            {
                strHTML = strHTML.Replace(strOldList[i], strNewList[i]);
            }
        }
        /// <summary>
        ///  document.write HTML，将HTML封转进 document.write 里
        ///  内部已实现对HTML的格式处理
        /// </summary>
        /// <param name="strHTML"></param>
        public static void JSDocWriteHTML(ref string strHTML)
        {
            JSDocWriteHTMLFormat(ref strHTML);
            strHTML = string.Format("document.write(\" {0} \")", strHTML);
        }
        /// <summary>
        /// 将HTML封转进 html的 script 标签里
        /// 内部已实现对HTML的格式处理
        /// </summary>
        /// <param name="strHTML"></param>
        public static void JSScriptWriteHTML(ref string strHTML)
        {
            JSDocWriteHTML(ref strHTML);
            strHTML = string.Format(" <script type=\"text/javascript\">{0}</script> ", strHTML);
        }
        /// <summary>
        /// 将HTML 转换成js 并 写进物理文件里
        /// 例如 document.write .......
        /// </summary>
        /// <param name="strHtml"></param>
        /// <param name="strPhysicalpath"></param>
        public static void WriteJSFile(string strHtml, string strPhysicalpath)
        {
            JSDocWriteHTML(ref strHtml);
            FileUtil.WriteFile(strPhysicalpath, strHtml);
        }
        /// <summary>
        /// 将HTML 转换成js 返回该JS
        /// 例如 document.write .......
        /// </summary>
        /// <param name="strHtml"></param>
        /// <returns></returns>
        public static string GetJSDocWriteHTML(string strHtml)
        {
            JSDocWriteHTML(ref strHtml);
            return strHtml;
        }
        /// <summary>
        /// 将Datatable转换为JavaScriptObject对象
        /// </summary>
        /// <param name="oTable"></param>
        /// <returns></returns>
        public static JavaScriptObject GetJsObjByDataTable(DataTable oTable)
        {
            JavaScriptObject oListJSObj = new JavaScriptObject();
            int index = 1;
            foreach (DataRow oRow in oTable.Rows)
            {
                JavaScriptObject oJSObjTemp = new JavaScriptObject();
                for (int i = 0; i < oRow.Table.Columns.Count; i++)
                {
                    oJSObjTemp.Add(oRow.Table.Columns[i].ColumnName, oRow[i].ToString());
                }
                oListJSObj.Add(index.ToString(), oJSObjTemp);
                index++;
            }
            return oListJSObj;
        }
    }
}
